// pages/order_detail/order_detail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id: '',
    order_info: [],
    k: '',
    u: '',
    is_show_time: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var order_id = options.order_id
    var key = wx.getStorageSync('key')
    var u = wx.getStorageSync('u')
    that.setData({
      k: key,
      u: u,
      order_id: order_id
    })
    that.requests(order_id)
  },
  onShow: function () {
    this.requests(this.data.order_id)
  },
  //确认订单 
  sure_order: function (e) {
    this.setData({
      con_nocancel: true,
    })
  },
  con_confirm: function () {
    var con_id = this.data.order_id
    this.sure_orderss(con_id)
    this.setData({
      con_nocancel: false,
    })
  },
  con_cancel: function () {
    this.setData({
      con_nocancel: false,
    })
  },
  //确认收货
  sure_orderss: function (con_id) {
    var that = this
    var order_id = con_id
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=confirmOrder&typ=json',
      method: 'GET',
      data: { k: that.data.k, u: that.data.u, order_id: order_id, user: 'buyer' },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.status == 200) {
          app.show('收货完成', 'success')
          that.requests(order_id)
        } else {
          app.show('收货失败', 'loading')
        }
        that.setData({
          del_nocancel: false,
        })
      }, fail: function (res) {
        app.show('数据加载失败', 'loading')
      }
    })
  },
  //取消订单
  cancel_order: function (e) {
    this.setData({
      nocancel: true,
    })
  },
  confirm: function () {
    var order_id = this.data.order_id
    this.cancel_orderss(order_id)
    this.setData({
      nocancel: false,
    })
  },
  cancel: function () {
    this.setData({
      nocancel: false,
    })
  },
  //取消订单
  cancel_orderss: function (order_id) {
    var that = this
    var order_id = order_id
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=orderCancel&typ=json',
      method: 'GET',
      data: { k: that.data.k, u: that.data.u, order_id: order_id, user: 'buyer' },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        if (res.data.status == 200) {
          app.show('取消成功', 'success')
          that.requests(order_id)
        } else {
          app.show('取消失败', 'loading')
        }
        that.setData({
          del_nocancel: false,
        })
      }, fail: function (res) {
        app.show('数据加载失败', 'loading')
      }
    })
  },
  //删除订单
  delete_order: function (e) {

    this.setData({
      del_nocancel: true
    })
  },
  del_confirm: function () {
    var order_id = this.data.order_id
    this.delete_orderss(order_id)
    this.setData({
      del_nocancel: false,
    })
  },
  del_cancel: function () {
    this.setData({
      del_nocancel: false,
    })
  },
  //删除订单
  delete_orderss: function (order_id) {
    var that = this
    var order_id = order_id
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=hideOrder&typ=json',
      method: 'GET',
      data: { k: that.data.k, u: that.data.u, order_id: order_id, user: 'buyer' },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        if (res.data.status == 200) {
          app.show('成功', 'success')
          wx.redirectTo({
            url: '../order_list/order_list',
          })
        } else {
          app.show('删除失败', 'loading')
        }
        that.setData({
          del_nocancel: false,
        })
      }, fail: function (res) {
        app.show('数据加载失败', 'loading')
      }
    })

  },

  link: function (e) {
    var goods_id = e.currentTarget.dataset.goods_id
    wx.redirectTo({
      url: '../product_detail/product_detail?goods_id=' + goods_id,
    })
  },
  //订单支付
  payorder: function (e) {
    var that = this
    var uorder_id = that.data.order_info.payment_number
    var openid = wx.getStorageSync('openid');
    
    wx.request({
      url: app.configData.paycenter.api_url + '/index.php?ctl=Pay&met=createWXOrder&typ=json',
      method: 'GET',
      data: {
        k: that.data.k,
        u: that.data.u,
        uorder_id: uorder_id,
        openid: openid,
        trade_type: 'WXAPP'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {

            wx.request({
              url: app.configData.paycenter.api_url + '/index.php?ctl=Pay&met=order_status&typ=json',
              method: 'GET',
              data: {
                k: that.data.k,
                u: that.data.u,
                order_id: uorder_id,
                buyer_id: that.data.u,
              },
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
                app.show('支付成功', 'success');
                that.requests(order_id)
              }
            })
          },

        });
      }
    });

  },
  //数据请求
  requests: function (order_id) {
    var that = this
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=virtual&act=detail&typ=json',
      method: 'GET',
      data: { k: that.data.k, u: that.data.u, order_id: order_id },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {


        if (res.data.status == 200) {
          that.setData({
            order_info: res.data.data,
            is_show_time: 0
          });
        } else {
          that.setData({
            is_show_time: 0
          });
        }

      }
    })
  },
  //取消退款
  cancel_refund: function (e) {
    var that = this
    wx.showModal({
      title: '',
      content: '确定取消退款',
      success: function (res) {
        if (res.confirm) {
          var order_return_id = e.currentTarget.dataset.order_return_id
          if (order_return_id) {
            wx.request({
              url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Service_Return&met=cancelRefund&typ=json',
              method: 'GET',
              data: { k: that.data.k, u: that.data.u, order_return_id: order_return_id },
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
                if (res.data.status == 200) {
                  that.requests(that.data.order_id)
                }
              }
            })
          }
        }
      }
    })

  },
  /* 减数 */
  delCount: function (e) {
    //商品id
    var that = this
   
    var k = wx.getStorageSync('key');
    var u = wx.getStorageSync('u');
    var e_id = e.target.id.substring(3);
    var arr = this.data.order_info;
   
   
      var del = arr.goods_list;
  	  var order_payment_amount = arr.order_payment_amount;
      for (var s in del) {
        var jian = del[s]
        if (jian.order_id == e_id) {
          
          if (jian.order_goods_num <= 1) {
            return;
          } else {
            jian.order_goods_num -= 1
  			order_payment_amount -= jian.order_goods_payment_amount
  			
  			jian.order_goods_amount = parseFloat((Number(jian.order_goods_num) * jian.order_goods_payment_amount))
            // 加载的使用进行网络访问，把需要的数据设置到data数据对象  
            wx.request({
              url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=editOrderNum&typ=json',
              method: 'GET',
              data: { k: k, u: u, order_id: jian.order_id, num: jian.order_goods_num, },
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
              }
            })
  			
          }
        }
      }
  	  
  	  //订单总数量与价格
  	  arr.order_payment_amount = parseFloat(order_payment_amount).toFixed(2);
  	  that.setData({
  	    order_info: that.data.order_info,
  	  })
  },
  /* 加数 */
  addCount: function (e) {
    //商品id
    var that = this
    var k = wx.getStorageSync('key');
    var u = wx.getStorageSync('u');
    var e_id = e.target.id.substring(3);
    var arr = this.data.order_info;
      var add = arr.goods_list;
  	  var order_payment_amount = arr.order_payment_amount;
      for (var s in add) {
        var jia = add[s]
        if (jia.order_id == e_id) {
          jia.order_goods_num = Number(jia.order_goods_num) + 1
  		order_payment_amount = parseFloat(order_payment_amount) + parseFloat(jia.order_goods_payment_amount)
  		jia.order_goods_amount = parseFloat((Number(jia.order_goods_num) * jia.order_goods_payment_amount))
          // if (jia.order_goods_num > jia.goods_base.goods_stock) {
          //   jia.order_goods_num = jia.goods_base.goods_stock
          //   that.show("不可超过商品库存")
          // } else {
            wx.request({
              url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=editOrderNum&typ=json',
              method: 'GET',
              data: {
                k: k,
                u: u,
                order_id: jia.order_id,
                num: jia.order_goods_num,
              },
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
              }
            })
          }
        // }
      }
  	  //订单总数量与价格
  	  arr.order_payment_amount = parseFloat(order_payment_amount).toFixed(2);
  	  that.setData({
  	    order_info: that.data.order_info,
  	  })
  	
  },
  /*购物车输入数量*/
  goodsNum: function (e) {
    //商品id
    var that = this
    var k = wx.getStorageSync('key');
    var u = wx.getStorageSync('u');
    var e_id = e.target.dataset.id;
    var inputNum = e.detail.value
    var arr = that.data.order_info;
   
      var del = arr.goods_list;
  	  var order_payment_amount = arr.order_payment_amount;
      for (var s in del) {
        var jian = del[s]
        if (jian.order_id == e_id) {
  			if (Number(inputNum) == 0) {
            // if (jian.flag == true) {
            //   that.data.total -= Number(jian.goods_num * jian.now_price);
            //   jian.goods_num = 1;
            //   that.data.total += Number(jian.goods_num * jian.now_price);
            // } else {
              
            // }
  			jian.order_goods_num = 1;
  			order_payment_amount = parseFloat(order_payment_amount) - parseFloat(jian.order_goods_amount) + parseFloat(jian.order_goods_payment_amount)
  			jian.order_goods_amount = parseFloat((Number(inputNum) * jian.order_goods_payment_amount))
  			arr[i].order_payment_amount = parseFloat(order_payment_amount).toFixed(2);
  			  that.setData({
  				order_info: that.data.order_info
  			  })
            return false
          }
  		  
  		  
  		  jian.order_goods_num = Number(inputNum)
  		  order_payment_amount = parseFloat(order_payment_amount) - parseFloat(jian.order_goods_amount) + parseFloat((Number(inputNum) * jian.order_goods_payment_amount))
  		  jian.order_goods_amount = parseFloat((Number(inputNum) * jian.order_goods_payment_amount))
  //         if (Number(inputNum) > Number(jian.goods_base.goods_stock)) {
  //           if (jian.flag == true) {
  //             that.data.total -= Number(jian.goods_num * jian.now_price);
  //             jian.goods_num = jian.goods_base.goods_stock
  //             that.data.total += Number(jian.goods_num * jian.now_price);
  //           } else {
  //             jian.goods_num = jian.goods_base.goods_stock
  //           }
  
  //           that.show("不可超过商品库存")
  //           that.setData({
  //             glistAll: that.data.glistAll,
  //             total: that.data.total,
  //             count_price: that.data.total.toFixed(2),
  //           })
  //         } else {
            // 加载的使用进行网络访问，把需要的数据设置到data数据对象  
            wx.request({
              url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=editOrderNum&typ=json',
              method: 'GET',
              data: { k: k, u: u, order_id: jian.order_id, num: inputNum, },
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
              }
            })
          // }
        }
      }
  	  //订单总数量与价格
  	  arr.order_payment_amount = parseFloat(order_payment_amount).toFixed(2);
  	  that.setData({
  	    order_info: that.data.order_info
  	  })
  },


})